{% extends 'base.html' %}
{% load static %}

{% block title %}设备品牌管理 - IoT智能家居控制系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-12">
        <!-- 页面标题和操作 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="bi bi-award me-2"></i>设备品牌管理</h2>
            <a href="{% url 'devices:brand_add' %}" class="btn btn-primary">
                <i class="bi bi-plus-circle me-1"></i>添加品牌
            </a>
        </div>

        <!-- 搜索和筛选 -->
        <div class="card mb-4">
            <div class="card-body">
                <form method="get" class="row g-3">
                    <div class="col-md-6">
                        <label for="search" class="form-label">搜索品牌</label>
                        <input type="text" class="form-control" id="search" name="search" 
                               value="{{ request.GET.search }}" placeholder="输入品牌名称或描述">
                    </div>
                    <div class="col-md-3">
                        <label for="ordering" class="form-label">排序方式</label>
                        <select class="form-select" id="ordering" name="ordering">
                            <option value="name" {% if request.GET.ordering == 'name' %}selected{% endif %}>按名称</option>
                            <option value="-created_at" {% if request.GET.ordering == '-created_at' %}selected{% endif %}>按创建时间</option>
                            <option value="-updated_at" {% if request.GET.ordering == '-updated_at' %}selected{% endif %}>按更新时间</option>
                        </select>
                    </div>
                    <div class="col-md-3 d-flex align-items-end">
                        <button type="submit" class="btn btn-outline-primary me-2">
                            <i class="bi bi-search me-1"></i>搜索
                        </button>
                        <a href="{% url 'devices:brand_list' %}" class="btn btn-outline-secondary">
                            <i class="bi bi-arrow-clockwise me-1"></i>重置
                        </a>
                    </div>
                </form>
            </div>
        </div>

        <!-- 品牌列表 -->
        <div class="card">
            <div class="card-header">
                <h6 class="mb-0">品牌列表 (共 {{ brands.count }} 个)</h6>
            </div>
            <div class="card-body">
                {% if brands %}
                    <div class="row g-4">
                        {% for brand in brands %}
                        <div class="col-lg-4 col-md-6">
                            <div class="card h-100 border-light shadow-sm">
                                <div class="card-body">
                                    <div class="d-flex align-items-start mb-3">
                                        {% if brand.logo %}
                                            <img src="{{ brand.logo.url }}" alt="{{ brand.name }}" 
                                                 class="rounded me-3" style="width: 60px; height: 60px; object-fit: cover;">
                                        {% else %}
                                            <div class="bg-light rounded d-flex align-items-center justify-content-center me-3" 
                                                 style="width: 60px; height: 60px;">
                                                <i class="bi bi-image text-muted fs-4"></i>
                                            </div>
                                        {% endif %}
                                        <div class="flex-grow-1">
                                            <h6 class="card-title mb-1">{{ brand.name }}</h6>
                                            <small class="text-muted">{{ brand.country|default:"未知" }}</small>
                                        </div>
                                    </div>
                                    
                                    {% if brand.description %}
                                    <p class="card-text small text-muted mb-3">{{ brand.description|truncatechars:80 }}</p>
                                    {% endif %}
                                    
                                    <div class="row text-center mb-3">
                                        <div class="col-6">
                                            <div class="border-end">
                                                <h6 class="text-primary mb-0">{{ brand.devices.count }}</h6>
                                                <small class="text-muted">设备数量</small>
                                            </div>
                                        </div>
                                        <div class="col-6">
                                            <h6 class="text-success mb-0">{{ brand.device_types.count }}</h6>
                                            <small class="text-muted">支持类型</small>
                                        </div>
                                    </div>
                                    
                                    {% if brand.website %}
                                    <div class="mb-3">
                                        <a href="{{ brand.website }}" target="_blank" class="text-decoration-none small">
                                            <i class="bi bi-globe me-1"></i>官方网站
                                        </a>
                                    </div>
                                    {% endif %}
                                </div>
                                <div class="card-footer bg-transparent">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <small class="text-muted">{{ brand.created_at|date:"Y-m-d" }}</small>
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'devices:brand_detail' brand.id %}" class="btn btn-outline-info">
                                                <i class="bi bi-eye"></i>
                                            </a>
                                            <a href="{% url 'devices:brand_edit' brand.id %}" class="btn btn-outline-primary">
                                                <i class="bi bi-pencil"></i>
                                            </a>
                                            <button class="btn btn-outline-danger" onclick="deleteBrand({{ brand.id }}, '{{ brand.name }}')">
                                                <i class="bi bi-trash"></i>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>

                    <!-- 分页 -->
                    {% if is_paginated %}
                    <nav aria-label="品牌分页" class="mt-4">
                        <ul class="pagination justify-content-center">
                            {% if page_obj.has_previous %}
                                <li class="page-item">
                                    <a class="page-link" href="?{% if request.GET.search %}search={{ request.GET.search }}&{% endif %}{% if request.GET.ordering %}ordering={{ request.GET.ordering }}&{% endif %}page={{ page_obj.previous_page_number }}">
                                        <i class="bi bi-chevron-left"></i>
                                    </a>
                                </li>
                            {% endif %}
                            
                            {% for num in page_obj.paginator.page_range %}
                                {% if page_obj.number == num %}
                                    <li class="page-item active">
                                        <span class="page-link">{{ num }}</span>
                                    </li>
                                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                                    <li class="page-item">
                                        <a class="page-link" href="?{% if request.GET.search %}search={{ request.GET.search }}&{% endif %}{% if request.GET.ordering %}ordering={{ request.GET.ordering }}&{% endif %}page={{ num }}">{{ num }}</a>
                                    </li>
                                {% endif %}
                            {% endfor %}
                            
                            {% if page_obj.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="?{% if request.GET.search %}search={{ request.GET.search }}&{% endif %}{% if request.GET.ordering %}ordering={{ request.GET.ordering }}&{% endif %}page={{ page_obj.next_page_number }}">
                                        <i class="bi bi-chevron-right"></i>
                                    </a>
                                </li>
                            {% endif %}
                        </ul>
                    </nav>
                    {% endif %}
                {% else %}
                    <div class="text-center py-5">
                        <i class="bi bi-award fs-1 text-muted"></i>
                        <h5 class="mt-3 text-muted">暂无品牌数据</h5>
                        <p class="text-muted">点击上方"添加品牌"按钮创建第一个设备品牌</p>
                        <a href="{% url 'devices:brand_add' %}" class="btn btn-primary">
                            <i class="bi bi-plus-circle me-1"></i>添加品牌
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// 删除品牌
function deleteBrand(brandId, brandName) {
    if (!confirm('确定要删除品牌 "' + brandName + '" 吗？\n\n注意：删除品牌可能会影响相关设备的显示。')) {
        return;
    }
    
    fetch('/devices/brands/' + brandId + '/delete/', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value
        }
    })
    .then(function(response) {
        if (response.ok) {
            location.reload();
        } else {
            alert('删除失败，请稍后重试');
        }
    })
    .catch(function(error) {
        console.error('Error:', error);
        alert('删除失败，请稍后重试');
    });
}

// 搜索表单自动提交
$(document).ready(function() {
    $('#search').on('input', function() {
        var self = this;
        clearTimeout(self.searchTimeout);
        self.searchTimeout = setTimeout(function() {
            $('form').submit();
        }, 500);
    });
    
    $('#ordering').on('change', function() {
        $('form').submit();
    });
});
</script>
{% endblock %}